package online.longzipeng.householdbillssystem.common.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @Author: lzp
 * @Date: 2021/2/7 18:58
 * 统一系统的返回结果集
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "统一返回结果集",description = "返回参数")
public class Result implements Serializable {

    @ApiModelProperty(value = "状态码",example = "200")
    private String code;
    @ApiModelProperty(value = "描述信息",example = "操作成功！！")
    private String message;
    @ApiModelProperty(value = "结果对象",example = "null")
    private Object data;

    public Result(ResponseEnum responseEnum, Object data) {
        this.code = responseEnum.getCode();
        this.message = responseEnum.getMessage();
        this.data = data;
    }

    public Result(ResponseEnum responseEnum) {
        this.code = responseEnum.getCode();
        this.message = responseEnum.getMessage();
    }

    /**
     * 返回操作成功通用结果集
     * @return
     */
    public static Result success(){
        Result result = new Result(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getMessage(),null);
        return result;
    }

    public static Result success(Object data){
        Result result = success();
        result.setData(data);
        return result;
    }

    /**
     * 返回操作失败通用结果集
     * @return
     */
    public static Result failed(){
        Result result = new Result(ResponseEnum.FAILED.getCode(),ResponseEnum.FAILED.getMessage(),null);
        return result;
    }

    public static Result failed(Object data){
        Result result = failed();
        result.setData(data);
        return result;
    }

}
